//
//  AlipayManager.m
//  ZPrograme
//
//  Created by 周成龙 on 16/6/17.
//  Copyright © 2016年 none. All rights reserved.
//

#import "AlipayManager.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>

#import "VDJConstant.h"
#import "MarkHeader.h"
#import "JQProgressHUD.h"

@implementation AlipayManager

+ (instancetype)sharedInstance
{
    static AlipayManager* instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [AlipayManager new];
    });
    return instance;
}

- (void)alipayWithHandleAction:(void (^)(void))handle
{
    /*
     *点击获取prodcut实例并初始化订单信息
     */
    
    /*============================================================================*/
    /*=======================需要填写商户app申请的===================================*/
    /*============================================================================*/
    NSString *partner = kPartner;
    NSString *seller = kSeller;
    NSString *privateKey = kPrivateKey;
    /*============================================================================*/
    /*============================================================================*/
    /*============================================================================*/
    
    
    //partner和seller获取失败,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少partner或者seller或者私钥。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        
        return;
    }
    
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.sellerID = seller;
    
    order.outTradeNO = _outTradeNO; //订单ID（由商家自行制定）
    order.subject = _subject; //商品标题
    order.body = _body; //商品描述
    order.totalFee = _totalFee; //商品价格
    
    order.notifyURL =  kNotifyURL; //回调URL
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showURL = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"ZPrograme";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    MyLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSString * str = resultDic[@"resultStatus"];
            if ([str isEqualToString:@"9000"])
            {
                // 处理事件
                handle();
            }
            else
            {
                [JQProgressHUD showErrorMessage:@"支付失败"];
            }
        }];
    }
}

#pragma mark - setter
- (void)setSubject:(NSString *)subject
{
	_subject = subject;
}

- (void)setTotalFee:(NSString *)totalFee
{
	_totalFee = totalFee;
}

- (void)setOutTradeNO:(NSString *)outTradeNO
{
	_outTradeNO = outTradeNO;
}

- (void)setBody:(NSString *)body
{
	_body = body;
}



@end
